package com.huilian.iotbox.admin.wap.controller;

import com.alibaba.fastjson.JSON;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.MyBankPhotoDto;
import com.huilian.iotbox.data.dto.UpLoadDto;
import com.huilian.iotbox.data.service.UploadService;
import com.huilian.iotbox.data.vo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 谢洛涛
 * @date 2021/3/16 17:44
 */
@Slf4j
@RestController
@RequestMapping("/api/upload")
public class UploadController extends UserInfoController {

    @Autowired
    private UploadService uploadService;

    /**
     * 按图片类型（身份证正面、反面、结算卡正面、反面等等）七牛图片上传
     */
    @PostMapping("/uploadQiNiuByType")
    public ServerResponse uploadQiNiuByType(MyBankPhotoDto myBankPhotoDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        log.info("uploadQiNiuByType" + JSON.toJSONString(userInfo));
        return uploadService.uploadQiNiuByType(myBankPhotoDto);
    }


    /**
     * 七牛图片上传
     */
    @PostMapping("/uploadQiniu")
    public ServerResponse uploadQiniu(MyBankPhotoDto myBankPhotoDto, HttpServletRequest request) throws Exception {
        UserInfo userInfo = super.getLoginUserInfo(request);
        log.info("uploadQiniu" + JSON.toJSONString(userInfo));
        return uploadService.uploadQiniu(myBankPhotoDto);
    }


    /**
     * 服务器图片上传
     */
    @PostMapping("/uploadImageToFTP")
    public ServerResponse uploadImageToFTP(@RequestBody MultipartFile image, HttpServletRequest request) throws IOException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        log.info("uploadImageToFTP" + JSON.toJSONString(userInfo));
        return uploadService.uploadImageToFTP(image.getInputStream());
    }

    /**
     * 服务器图片删除
     */
    @DeleteMapping("/deleteByFileNameToFTP")
    public ServerResponse deleteByFileNameToFTP(@RequestBody UpLoadDto upLoadDto, HttpServletRequest request) throws IOException {
        UserInfo userInfo = super.getLoginUserInfo(request);
        log.info("deleteByFileNameToFTP" + JSON.toJSONString(userInfo));
        return uploadService.deleteByFileNameToFTP(upLoadDto.getFileName());
    }
}
